package db_utils

import (
	"dgo/goutils/utils"
	"io"
	"strings"
)

var (
	BUF_Comma     []byte = []byte(",")
	BUF_Semicolon []byte = []byte(";")
	BUF_Enter     []byte = []byte("\r\n")
)

type DataBackup struct {
	w io.Writer
}

func (this *DataBackup) ConfigWriter(w io.Writer) {
	this.w = w
}

func (this *DataBackup) WriteTable(table string, keyfields string, columns []string) {
	this.w.Write([]byte(table))
	this.w.Write(BUF_Comma)
	this.w.Write([]byte(strings.ToLower(keyfields)))
	this.w.Write(BUF_Enter)
	colstr := strings.ToLower(strings.Join(columns, ","))
	this.w.Write([]byte(colstr))
	this.w.Write(BUF_Enter)
}

func (this *DataBackup) WriteRow(row []SqlValue) {
	this.w.Write([]byte("["))
	for i := 0; i < len(row); i++ {
		if i > 0 {
			this.w.Write(BUF_Comma)
		}
		this.w.Write([]byte(utils.ToJSONValString(row[i].AsVar(nil))))
	}
	this.w.Write([]byte("]"))
	this.w.Write(BUF_Enter)
}
